home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / intrlib1.zip / INTR_GEN.C < prev    next >
C/C++ Source or Header  |  1992-02-17  |  5KB  |  147 lines

  1. /******************************************************************************
  2. * Iteraction library - generic functions.                      *
  3. *                                          *
  4. *                    Written by Gershon Elber,  Oct. 1990  *
  5. *******************************************************************************
  6. * History:                                      *
  7. *  7 Oct 90 - Version 1.0 by Gershon Elber.                      *
  8. ******************************************************************************/
  9.  
  10. #include <math.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14.  
  15. #ifdef __MSDOS__
  16. #include <alloc.h>
  17. #include "mousedrv.h"
  18. #endif /* __MSDOS__ */
  19.  
  20. #include "intr_loc.h"
  21. #include "intr_gr.h"
  22.  
  23. #ifdef __TURBOC__                 /* Only for TC++ 1.0 and above. */
  24. #define __DEBUG_MALLOC__
  25. #endif /* __TURBOC__ */
  26.  
  27. static int InputDevicesUsed = 0;
  28.  
  29. /*****************************************************************************
  30. * Routine to initialize the library. Must be called before any other call to *
  31. * any other function in the library.                         *
  32. *****************************************************************************/
  33. void IntrInit(void)
  34. {
  35.     if (InputDevicesUsed != 0) {
  36.     /* This is NOT the first time IntrInit is Being called and input     */
  37.         /* devices has been initialized. Reinitialize them now.             */
  38.     IntrSetInputDevice(InputDevicesUsed); /* Reenable all input devices. */
  39.     }
  40.  
  41.     GRInitGraph();
  42.  
  43.    _IntrAllocColors();
  44. }
  45.  
  46. /*****************************************************************************
  47. * Routine to close the library. Should be called in the end before the       *
  48. * programs quits.                                                   *
  49. *****************************************************************************/
  50. void IntrClose(void)
  51. {
  52.     _IntrRestoreAll();               /* In case something is being poped up. */
  53.  
  54.     GRCloseGraph();
  55.  
  56.     InputDevicesUsed = _IntrActiveDevices;
  57.     IntrSetInputDevice(0);               /* Disable all input devices. */
  58. }
  59.  
  60. #ifdef __DEBUG_MALLOC__
  61. /*****************************************************************************
  62. * My Routine to    allocate dynamic memory. All program requests must call this *
  63. * routine (no direct call to malloc). Dies if no memory.             *
  64. *****************************************************************************/
  65. static void AllocError(const char *Msg, VoidPtr *p)
  66. {
  67.     char s[128];
  68.  
  69.     sprintf(s, "%s, Ptr = %p\n", Msg, p);
  70.     FATAL_ERROR(s);
  71. }
  72. #endif /* __DEBUG_MALLOC__ */
  73.  
  74. /*****************************************************************************
  75. * My Routine to    allocate dynamic memory. All program requests must call this *
  76. * routine (no direct call to malloc). Dies if no memory.             *
  77. *****************************************************************************/
  78. VoidPtr _IntrMalloc(unsigned size)
  79. {
  80.     VoidPtr p;
  81.  
  82.     if ((p = (VoidPtr) malloc(size)) != NULL) {
  83.         return p;
  84.     }
  85.  
  86.     IntrFatalError("Not enough memory, exit.");
  87.  
  88.     return NULL;                    /* Make warnings silent. */
  89. }
  90.  
  91. /*****************************************************************************
  92. * My Routine to    free dynamic memory. All program requests must call this     *
  93. * routine (no direct call to free).                         *
  94. *****************************************************************************/
  95. void _IntrFree(VoidPtr p)
  96. {
  97. #ifdef __DEBUG_MALLOC__
  98.     switch (heapcheck()) {
  99.     case _HEAPCORRUPT:
  100.         AllocError("Heap is corrupted", p);
  101.         break;
  102.     case _BADNODE:
  103.         AllocError("Attempt to free a bogus pointer", p);
  104.         break;
  105.     case _FREEENTRY:
  106.         AllocError("Attempt to free an already freed pointer", p);
  107.         break;
  108.     case _HEAPOK:
  109.     case _HEAPEMPTY:
  110.     case _USEDENTRY:
  111.         break;
  112.     default:
  113.         AllocError("Allocation error", p);
  114.         break;
  115.  
  116.     }
  117. #endif /* __DEBUG_MALLOC__ */
  118.  
  119.     free(p);
  120. }
  121.  
  122. /******************************************************************************
  123. * Fatal Error handler - print the given message to stderr and dies.          *
  124. ******************************************************************************/
  125. void IntrFatalError(char *Msg)
  126. {
  127.     IntrClose();
  128.  
  129.     fprintf(stderr, "INTR_LIB: %s\n", Msg);
  130.  
  131.     exit(5);
  132. }
  133.  
  134. #ifdef __TURBOC__
  135.  
  136. /*****************************************************************************
  137. * Define dummy floating point routine to force linking of floating point     *
  138. * package.                                     *
  139. *****************************************************************************/
  140. IntrRType __DummyFloatingPointRoutine(IntrRType r)
  141. {
  142.     return exp(r);
  143. }
  144.  
  145. #endif /* __TURBOC__ */
  146.  
  147.